home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Audio, Video & Photo / Songbird 0.7.0 / Songbird_0.7.0_windows-i686-msvc8.exe / components / sbLocalDatabaseMigrationHelper.js < prev    next >
Text File  |  2008-08-19  |  8KB  |  268 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. //
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2008 POTI, Inc.
  8. // http://songbirdnest.com
  9. //
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the "GPL").
  12. //
  13. // Software distributed under the License is distributed
  14. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  15. // express or implied. See the GPL for the specific language
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc.,
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. //
  23. // END SONGBIRD GPL
  24. //
  25. */
  26.  
  27. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  28. Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
  29. Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
  30. Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
  31. Components.utils.import("resource://app/jsmodules/SBJobUtils.jsm");
  32.  
  33. const Cc = Components.classes;
  34. const Ci = Components.interfaces;
  35. const Cr = Components.results;
  36.  
  37. const SBLDBCOMP = "@songbirdnest.com/Songbird/Library/LocalDatabase/";
  38.  
  39. function d(s) {
  40.   //dump("------------------> sbLocalDatabaseMigration " + s + "\n");
  41. }
  42.  
  43. function TRACE(s) {
  44.   //dump("------------------> sbLocalDatabaseMigration " + s + "\n");
  45. }
  46.  
  47. function sbLocalDatabaseMigrationHelper()
  48. {
  49.   SBJobUtils.JobBase.call(this);
  50.   this._migrationHandlers = {};
  51. }
  52.  
  53. //-----------------------------------------------------------------------------
  54. // sbLocalDatabaseMigration Implementation
  55. //-----------------------------------------------------------------------------
  56.  
  57. sbLocalDatabaseMigrationHelper.prototype = {
  58.   __proto__: SBJobUtils.JobBase.prototype,
  59.   
  60.   classDescription: "Songbird Local Database Library Migration",
  61.   classID:          Components.ID("{744f6217-4cb9-4929-8d1d-72492c1b8c83}"),
  62.   contractID:       SBLDBCOMP + "MigrationHelper;1",
  63.   implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
  64.  
  65.   // needs to be DOM_OBJECT to allow remoteAPI to access it.
  66.   flags: Ci.nsIClassInfo.DOM_OBJECT,
  67.  
  68.   _initialized: false,
  69.   
  70.   _securityMixin: null,
  71.   _initializedSCC: false,
  72.   
  73.   _publicWProps: [ "" ],
  74.   _publicRProps: [ "internal:latestSchemaVersion",
  75.                    "classinfo:classDescription",
  76.                    "classinfo:contractID",
  77.                    "classinfo:classID",
  78.                    "classinfo:implementationLanguage",
  79.                    "classinfo:flags" ],
  80.   _publicMethods: [ "internal:canMigrate",
  81.                     "internal:migrate"],
  82.   _publicInterfaces: [ Ci.nsISupports,
  83.                        Ci.nsIClassInfo,
  84.                        Ci.nsISecurityCheckedComponent,
  85.                        Ci.sbILocalDatabaseMigrationHelper,
  86.                        Ci.sbIJobProgress,
  87.                        Ci.sbIJobCancelable ],
  88.   
  89.   _latestSchemaVersion: 8,
  90.   _lowestFromSchemaVersion: Number.MAX_VALUE,
  91.   
  92.   _migrationHandlers:   null,
  93.  
  94.   //
  95.   // Local methods
  96.   //
  97.  
  98.   _init: function sbLDBM_init() {
  99.     this._getMigrationHandlerList();
  100.     this._initialized = true;
  101.   },
  102.  
  103.   _getMigrationHandlerList: function sbLDBM_getMigrationHandlerList() {
  104.     this._migrationHandlers = {};
  105.     
  106.     for(let contractID in Cc) {
  107.       if(contractID.indexOf(SBLDBCOMP + "Migration/Handler") == 0) {
  108.  
  109.         let migrationHandler = 
  110.           Cc[contractID].createInstance(Ci.sbILocalDatabaseMigrationHandler);
  111.         let migrationHandlerKey = migrationHandler.fromVersion + 
  112.                                   "," + 
  113.                                   migrationHandler.toVersion;
  114.         this._migrationHandlers[migrationHandlerKey] = migrationHandler;
  115.         
  116.         if(this._lowestFromSchemaVersion > migrationHandler.fromVersion) {
  117.           this._lowestFromSchemaVersion = migrationHandler.fromVersion;
  118.         }
  119.       }
  120.     }
  121.   },
  122.  
  123.   // 
  124.   // sbILocalDatabaseMigration
  125.   // 
  126.  
  127.   get latestSchemaVersion() {
  128.     return this._latestSchemaVersion;
  129.   },
  130.  
  131.   canMigrate: function sbLDBM_canMigrate(aFromVersion, aToVersion) {
  132.     if(!this._initialized) {
  133.       this._init();
  134.     }
  135.     
  136.     var oldVersion = aFromVersion;
  137.     var newVersion = oldVersion + 1;
  138.     
  139.     for(let i = 0; i < aToVersion - aFromVersion; ++i) {
  140.       let key = oldVersion + "," + newVersion;
  141.       
  142.       if(!(key in this._migrationHandlers)) {
  143.         return false;
  144.       }
  145.       
  146.       oldVersion = newVersion;
  147.       newVersion += 1;
  148.     }
  149.     
  150.     return true;
  151.   },
  152.  
  153.   migrate: function sbLDBM_migrate(aFromVersion, aToVersion, aLibrary) {
  154.     if(!this._initialized) {
  155.       this._init();
  156.     }
  157.     
  158.     var oldVersion = aFromVersion;
  159.     var newVersion = oldVersion + 1;
  160.     
  161.     for(let i = 0; i < aToVersion - aFromVersion; ++i) {
  162.       let key = oldVersion + "," + newVersion;
  163.       
  164.       if(!(key in this._migrationHandlers)) {
  165.         throw Cr.NS_ERROR_UNEXPECTED;
  166.       }
  167.       
  168.       this._migrationHandlers[key].migrate(aLibrary);
  169.       
  170.       oldVersion = newVersion;
  171.       newVersion += 1;
  172.     }
  173.     
  174.     return;
  175.   },
  176.  
  177.  
  178.   //
  179.   // nsISupports
  180.   //
  181.  
  182.   QueryInterface: XPCOMUtils.generateQI([
  183.     Ci.sbILocalDatabaseMigrationHelper,
  184.     Ci.sbIJobProgress,
  185.     Ci.sbIJobCancelable,
  186.     Ci.nsIClassInfo,
  187.     Ci.nsISecurityCheckedComponent,
  188.     Ci.sbISecurityAggregator
  189.   ]),
  190.  
  191.  
  192.   // 
  193.   // nsIClassInfo
  194.   //
  195.  
  196.   getInterfaces: function sbLDBM_getInterfaces(count) {
  197.     var interfaces = [Ci.sbILocalDatabaseMigrationHelper, 
  198.                       Ci.nsIClassInfo,
  199.                       Ci.sbIJobProgress,
  200.                       Ci.sbIJobCancelable,
  201.                       Ci.nsISecurityCheckedComponent,
  202.                       Ci.sbISecurityAggregator,
  203.                       Ci.nsISupports];
  204.     
  205.     count.value = interfaces.length;
  206.     
  207.     return interfaces;
  208.   },
  209.  
  210.   getHelperForLanguage: function sbLDBM_getHelperForLanguage(aLanguage) {
  211.     return null;
  212.   },
  213.  
  214.  
  215.   //
  216.   // nsISecurityCheckedComponent -- implemented by the security mixin
  217.   //
  218.  
  219.   _initSCC: function sbLDBM__initSCC() {
  220.     this._securityMixin = Cc["@songbirdnest.com/remoteapi/security-mixin;1"]
  221.                             .createInstance(Ci.nsISecurityCheckedComponent);
  222.  
  223.     // initialize the security mixin with the cleared methods and props
  224.     this._securityMixin
  225.              .init(this, this._publicInterfaces, this._publicInterfaces.length,
  226.                          this._publicMethods, this._publicMethods.length,
  227.                          this._publicRProps, this._publicRProps.length,
  228.                          this._publicWProps, this._publicWProps.length,
  229.                          false);
  230.  
  231.     this._initializedSCC = true;
  232.   },
  233.  
  234.   canCreateWrapper: function sbLDBM_canCreateWrapper(iid) {
  235.     if (! this._initializedSCC)
  236.       this._initSCC();
  237.     return this._securityMixin.canCreateWrapper(iid);
  238.   },
  239.  
  240.   canCallMethod: function sbLDBM_canCallMethod(iid, methodName) {
  241.     if (! this._initializedSCC)
  242.       this._initSCC();
  243.     return this._securityMixin.canCallMethod(iid, methodName);
  244.   },
  245.  
  246.   canGetProperty: function sbLDBM_canGetProperty(iid, propertyName) {
  247.     if (! this._initializedSCC)
  248.       this._initSCC();
  249.     return this._securityMixin.canGetProperty(iid, propertyName);
  250.   },
  251.  
  252.   canSetProperty: function sbLDBM_canSetProperty(iid, propertyName) {
  253.     if (! this._initializedSCC)
  254.       this._initSCC();
  255.     return this._securityMixin.canSetProperty(iid, propertyName);
  256.   }
  257. };
  258.  
  259. //
  260. // Module
  261. //
  262.  
  263. function NSGetModule(compMgr, fileSpec) {
  264.   return XPCOMUtils.generateModule([
  265.     sbLocalDatabaseMigrationHelper
  266.   ]);
  267. }
  268.